﻿/***************************************************** 
** 命名空间: ShieldSimulatorWebDAL 
** 文 件 名：DataDAL 
** 内容简述：这个不知道该写什么东西进来，似乎dal原本是写存取数据库代码的地方
** 版    本：V1.0 
** 创 建 人：伍兆祥
** 创建日期：2018/4/10 10:56:25 
** 修改记录：无
日期        版本      修改人    修改内容    
*****************************************************/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShieldSimulatorWebModel;

namespace ShieldSimulatorWebDAL
{
    /// <summary>
    /// 数据持久层
    /// </summary>
    public class DataDAL
    {
        /// <summary>
        /// 实际数据存储
        /// </summary>
        private static List<SimDataModel> _buffer = new List<SimDataModel>();

        /// <summary>
        /// 提取（仿真）一条数据
        /// </summary>
        /// <param name="model">提取（仿真）的数据</param>
        /// <returns>返回成功或者失败</returns>
        public bool AddData(SimDataModel model)
        {
            try
            {
                lock (_buffer)
                {
                    _buffer.Add(model);
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public List<SimDataModel> GetAllList()
        {
            lock (_buffer)
            {
                var list = _buffer;
                return _buffer;
            }
        }
        /// <summary>
        /// 获取最新数据
        /// </summary>
        /// <param name="Number">获取的数量</param>
        /// <returns></returns>
        public List<SimDataModel> GetLastestDate(int Number)
        {
            /*按时间倒序排序 裁切前Number个 take裁剪出来的是枚举类型，故还要转换一次*/
            var list = GetAllList().OrderByDescending(i => i.GenerateTime).Take(Number).ToList();
            return list;
        }

        /// <summary>
        /// 获取时间段内的列表数据
        /// </summary>
        /// <param name="StartTime">开始时间</param>
        /// <param name="EndTime">结束时间</param>
        /// <returns></returns>
        public List<SimDataModel> GetSectionDataByRange(DateTime StartTime, DateTime EndTime)
        {
            var list = GetAllList().FindAll(i => i.GenerateTime >= StartTime && i.GenerateTime <= EndTime);
            return list;
        }

        /// <summary>
        /// 通过id删除某条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteDataById(int id)
        {
            lock (_buffer)
            {
                try
                {
                    _buffer.RemoveAt(_buffer.FindIndex(t => t.Id == id));
                    return true;
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    return false;
                }

            }
        }

        /// <summary>
        /// 通过model更新某条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool UpdateDataByModel(SimDataModel model)
        {
            lock (_buffer)
            {
                try
                {
                    var oldData = _buffer.Find(t => t.Id == model.Id);//引用类型，直接赋值对应值就相当于更改数据了
                    //如果发现值相同，就不往后执行了
                    if (oldData.GroundPressure == model.GroundPressure && oldData.Temperature == model.Temperature && oldData.Flow == model.Flow) return false;
                    oldData.GroundPressure = model.GroundPressure;
                    oldData.Temperature = model.Temperature;
                    oldData.Flow = model.Flow;
                    return true;
                }
                catch (ArgumentNullException ex)
                {
                    return false;
                }

            }
        }

        /// <summary>
        /// 通过数组id删除某条数据
        /// </summary>
        /// <param name="arrayId"></param>
        /// <returns></returns>
        public bool DeleteDataByIdArray(int[] arrayId)
        {
            lock (_buffer)
            {
                try
                {
                    foreach (var id in arrayId)
                    {
                        _buffer.RemoveAt(_buffer.FindIndex(t => t.Id == id));
                    }
                    return true;
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    return false;
                }

            }
        }

        /// <summary>
        /// 通过modellist更新多条数据
        /// </summary>
        /// <param name="modellist"></param>
        /// <returns></returns>
        public bool UpdataDataByModelList(List<SimDataModel> modellist)
        {
            lock (_buffer)
            {
                try
                {
                    foreach (var p in modellist)
                    {
                        var oldData = _buffer.Find(t => t.Id == p.Id);//引用类型，直接赋值对应值就相当于更改数据了
                        ////如果发现值相同，就不往后执行了  更新多条数据时候可以不这么操作
                        //if (oldData.GroundPressure == p.GroundPressure && oldData.Temperature == p.Temperature && oldData.Flow == p.Flow) return false;
                        oldData.GroundPressure = p.GroundPressure;
                        oldData.Temperature = p.Temperature;
                        oldData.Flow = p.Flow;
                    }
                    return true;
                }
                catch (ArgumentException ex)
                {
                    return false;
                }

            }
        }
    }
}
